package test;

import utils.Digital;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class EightDigitalProblem {
    public static void main(String[] args) {
        Digital digital = new Digital(new int[][]{{2, 8, 3}, {1, 6, 4}, {7, 0, 5}});
        Digital target = new Digital(new int[][]{{1, 2, 3}, {8, 0, 4}, {7, 6, 5}});
        digital.setCount(target);
        digital.x = 2;
        digital.y = 1;

        target.flag = 0;
        target.x = 1;
        target.y = 1;

        start(digital, target);

    }

    private static void start(Digital digital, Digital target) {
        LinkedList<Digital> open = new LinkedList<>();
        Set<Digital> closed = new HashSet<>();
        open.addFirst(digital);
        int count = 0;
        while (!open.isEmpty() && count < 30) {
            count++;
            Digital node = open.pollFirst();
            if (node.flag == 0 || target.equals(node)) {
                System.out.println(closed);
                System.out.println(node);
                System.out.println("结束" + "\t 一共进行了：" + count + "次");
                break;
            }
            if (closed.contains(node)) {
                continue;
            }
            Digital[] tmp = {node.up(), node.down(), node.left(), node.right()};
            for (Digital d : tmp) {
                if (d == null) {
                    continue;
                }
                if (!closed.contains(d) && !open.contains(d)) {
                    d.setCount(target);
                    open.add(d);
                }
            }
            //排序
            Collections.sort(open);
            closed.add(node);
        }
    }
}
